package com.example.mybatisdemo.controllers;

import com.example.core.libs.JSONResult;
import com.example.mybatisdemo.services.IdSequenceService;
import com.example.mybatisdemo.utils.SnUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

@RestController
public class IdSequenceController {
    @Autowired
    IdSequenceService idSequenceService;
    @Autowired
    SnUtils snUtils;

    final static int MAX_NUM = 1000;

    private static int corePoolSize = Runtime.getRuntime().availableProcessors();
    //创建线程池
    private static ThreadPoolExecutor executor = new ThreadPoolExecutor(
            corePoolSize,
            corePoolSize + 1,
            10l,
            TimeUnit.SECONDS,
            new LinkedBlockingQueue<>(MAX_NUM));

    @RequestMapping("/create")
    public JSONResult create() {
        for (int i = 0; i < MAX_NUM; i++) {
            Runnable runnable = () -> {
                String sn = snUtils.buildSn(SnUtils.BusinessCode.SHOPPING_ORDER);
                System.out.println(sn);
            };
            executor.execute(runnable);
        }
        return new JSONResult();
    }
}
